//
//  BGOneCell.m
//  bigMall
//
//  Created by ios on 16/9/18.
//  Copyright © 2016年 thinklev. All rights reserved.
//

#import "BGOneCell.h"
#import "BGReadStepTwoModel.h"

@interface BGOneCell () {
    
    NSMutableArray *dataArray;
    BOOL btnState;
    NSInteger Index;
    NSMutableArray *btnArr;
    UILabel *lab110;
    UITextField *filed110;
    UILabel *titleLab;
    int index;
    NSMutableArray *idArr;
    NSMutableArray *default_idArr;
    
}

@end

@implementation BGOneCell

//- (void)setArr:(NSMutableArray *)arr {
//   
//    dataArray = [NSMutableArray array];
//    idArr = [NSMutableArray array];
//    for (BGReadAddGoodsStepTWoModelResAttrInfo *model in arr) {
//        [dataArray addObject:model.value];
//        [idArr addObject:model.idStr];
//    }
//    
//}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier dataArr:(NSMutableArray *)marr default_value:(NSArray *)default_valueArr index:(NSIndexPath *)indexPath{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        btnArr = [NSMutableArray array];
        index = (int)indexPath.row;
//        for (UIView *v in self.contentView.subviews) {
//            [v removeFromSuperview];
//        }
        
        dataArray = [NSMutableArray array];
        idArr = [NSMutableArray array];
        for (BGReadStepTwoModelArrDetail *model in marr) {
            [dataArray addObject:model.value];
            [idArr addObject:model.ID];
        }
        
        default_idArr = [NSMutableArray array];
        for (BGReadStepTwoModelArrDetail *model in default_valueArr) {
            [default_idArr addObject:model.ID];
        }
        
        //修饰线
        UIView *v1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 1)];
        v1.backgroundColor = dockwhite_;
        [self.contentView addSubview:v1];
        
        titleLab = [[UILabel alloc] initWithFrame:CGRectMake(21, 6, 100, 21)];
        titleLab.text = self.titleText;
        //设置标题的大小
        CGSize biaotiSize = [titleLab.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:titleLab.font,NSFontAttributeName, nil]];
        titleLab.width = biaotiSize.width;
        titleLab.height = biaotiSize.height;
        titleLab.font = [UIFont systemFontOfSize:16];
        [self.contentView addSubview:titleLab];
        
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 32, [UIScreen mainScreen].bounds.size.width, 1)];
        view.backgroundColor = dockwhite_;
        [self.contentView addSubview:view];
        
        //创建多选按钮啊
        int colCount = 3;
        CGFloat width = ([UIScreen mainScreen].bounds.size.width - 46 - 32) / 3;
        CGFloat heigh = 30;
        CGFloat maginX = 23;
        CGFloat maginY = 16;
        for (int i = 0 ; i < dataArray.count; i++) {
            int row = i / colCount;
            int col = i % colCount;
            CGFloat x = maginX + col * (width + 16);
            CGFloat y = maginY + 30 + row * (heigh + maginY);
            UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(x, y, width, heigh)];
            btn.tag = 10 + [idArr[i] intValue];
            
            [btn setTitle:dataArray[i] forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [btn.titleLabel setFont:[UIFont systemFontOfSize:14]];
            [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
            
            // 按钮边框宽度
            btn.layer.borderWidth = 1.6;
            // 设置圆角
            btn.layer.cornerRadius = 10;
            // 设置颜色空间为rgb，用于生成ColorRef
            
            CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
            // 新建一个红色的ColorRef，用于设置边框（四个数字分别是 r, g, b, alpha）
            CGColorRef borderColorRef = CGColorCreate(colorSpace,(CGFloat[]){ 0, 0, 0, 1 });
            // 设置边框颜色
            btn.layer.borderColor = borderColorRef;
            
            [btnArr addObject:btn];
            
            [self.contentView addSubview:btn];
            
            for (NSString *idStr in default_idArr) {
                
//                NSLog(@"idStr === %@",idStr);
                
                if ([idStr intValue] == btn.tag - 10) {
                    btn.selected = YES;
                    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
                    // 设置颜色空间为rgb，用于生成ColorRef
                    
                    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
                    // 新建一个红色的ColorRef，用于设置边框（四个数字分别是 r, g, b, alpha）
                    CGColorRef borderColorRef = CGColorCreate(colorSpace,(CGFloat[]){ 255, 0, 0, 1 });
                    // 设置边框颜色
                    btn.layer.borderColor = borderColorRef;
                    
                    Index = btn.tag;
                }
            }
            
            
        }

            }
      return self;
}
- (void)setTitleText:(NSString *)titleText {
    titleLab.text = titleText;
}
- (void)layoutSubviews {
    
    //设置标题的大小
    CGSize biaotiSize = [titleLab.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:titleLab.font,NSFontAttributeName, nil]];
    titleLab.width = biaotiSize.width;
    titleLab.height = biaotiSize.height;

}
- (void)btnClick:(UIButton*)btn {
    
    
    btn.selected = !btn.selected;
    if (btn.selected) {
        
        [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        // 设置颜色空间为rgb，用于生成ColorRef
        
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
        // 新建一个红色的ColorRef，用于设置边框（四个数字分别是 r, g, b, alpha）
        CGColorRef borderColorRef = CGColorCreate(colorSpace,(CGFloat[]){ 255, 0, 0, 1 });
        // 设置边框颜色
        btn.layer.borderColor = borderColorRef;
        
        Index = btn.tag;
        
        //
        
    } else {
        
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        // 设置颜色空间为rgb，用于生成ColorRef
        
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
        // 新建一个红色的ColorRef，用于设置边框（四个数字分别是 r, g, b, alpha）
        CGColorRef borderColorRef = CGColorCreate(colorSpace,(CGFloat[]){ 0, 0, 0, 1 });
        // 设置边框颜色
        btn.layer.borderColor = borderColorRef;
        
    }
    
    NSMutableArray *viewsArr = [NSMutableArray array];
    
    viewsArr = [NSMutableArray arrayWithArray:self.subviews];
    
    for (UIView *vie in viewsArr) {
        
        if ([vie isKindOfClass:[UIButton class]]) {
            
            UIButton *b = (UIButton *)vie;
            
            
            if ([b isEqual:btn]) {
                
                NSLog(@"1");
                
            }
            
        }
        
    }
    
    if ([_delegate respondsToSelector:@selector(valueChange:index:)]) {
        [_delegate valueChange:btn index:self.attr_id];
//        NSLog(@"self.attr_id === %@",self.attr_id);
    }
    
}
@end
